package org.rabbitmq.rabbitextensions.config;

import org.rabbitmq.rabbitextensions.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class DlConfig {
    //正常
//    @Bean("normQueue")
//    public Queue normQueue(){
//        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
//                //配置死信连接的交换机队列
//                .deadLetterExchange(Constants.DL_EXCHANGE)
//                //配置死信的routing
//                .deadLetterRoutingKey("dl")
//                //设置过期时间,过期时间到了,消息还未从队列中移除,就被视为死信,就要送到死信队列中
//                .ttl(10000)
//                //设置队列长度
//                .maxLength(10L)
//                .build();
//    }
    @Bean("normQueue")
    public Queue normQueue(){
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                //配置死信连接的交换机队列
                .deadLetterExchange(Constants.DL_EXCHANGE)
                //配置死信的routing
                .deadLetterRoutingKey("dl")
                .build();
    }
    @Bean("normExchange")
    public DirectExchange normExchange(){
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).build();
    }
    @Bean("bindingNorm")
    public Binding bindingNorm(@Qualifier("normExchange") Exchange exchange, @Qualifier("normQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("norm").noargs();
    }
    //死信
    @Bean("dlQueue")
    public Queue dlQueue(){
        return QueueBuilder.durable(Constants.DL_QUEUE).build();
    }
    @Bean("dlExchange")
    public DirectExchange dlExchange(){
        return ExchangeBuilder.directExchange(Constants.DL_EXCHANGE).build();
    }
    @Bean("bindingDl")
    public Binding bindingDl(@Qualifier("dlExchange") Exchange exchange,@Qualifier("dlQueue") Queue queue){
        return BindingBuilder.bind(queue).to(exchange).with("dl").noargs();
    }


}
